package com.changanw.code21;

import com.changanw.tonumplus.ListNode;
import com.changanw.tonumplus.Solution;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author changanw
 * @description
 * @date 2023/7/11 22:50
 */
public class Code21 {
  //public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
    //ListNode pre = new ListNode(0);
    //定义一个可移动的指针，用来指向存储两个数之和的位置
    //ListNode cur = pre;
    //List<Integer> resList = new ArrayList<>();
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
      if (l1 == null) {
        return l2;
      } else if (l2 == null) {
        return l1;
      } else if (l1.val < l2.val) {
        l1.next = mergeTwoLists(l1.next, l2);
        return l1;
      } else {
        l2.next = mergeTwoLists(l1, l2.next);
        return l2;
      }
    }

    
    /*while (list1 != null){
      int x = list1.val;
      resList.add(x);
      list1 = list1.next;
    }
    while (list2 != null){
      int x = list2.val;
      resList.add(x);
      list2 = list2.next;
    }*/
    //resList.sort(Comparator.naturalOrder());
//    Arrays.sort(resList.toArray());
//    for (Integer integer : resList) {
//      cur.next = new ListNode(integer);
//      cur = cur.next;
//    }
//    return pre.next;

  //}

  public static void main(String[] args) {
    ListNode l1 = new ListNode(1,new ListNode(2,new ListNode(4)));
    ListNode l2 = new ListNode(1,new ListNode(3,new ListNode(4)));
    //ListNode l1 = new ListNode(2);
    //ListNode l2 = new ListNode(1);
    ListNode listNode = Code21.mergeTwoLists(l1, l2);
    System.out.println(listNode);
  }


  static class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
      this.val = val;
    }

    ListNode(int val, ListNode next) {
      this.val = val;
      this.next = next;
    }
  }
}
